﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.IO;
using TwitterClient2.ImageProcessing;
using TwitterClient2.Controller;

namespace TwitterClient2.Components
{
    class PanelStartBackground : Panel
    {

        private TwitterClientController controller = TwitterClientController.getInstance();
        protected Bitmap backBuffer;
        protected Image constantAlphaImage;
        private byte alpha = 0;
        private PictureBox pictureTwitterBird;
        private Timer TimerLogoAnimation;
        private int timer = 0;

        public PanelStartBackground()
        {
            this.InitializeComponent();
            pictureTwitterBird.Enabled = true;
            backBuffer = new Bitmap(200, 59, System.Drawing.Imaging.PixelFormat.Format32bppRgb);




            String sRes = "TwitterClient2.Images.twitterLogo.png";
            Assembly aAsm = this.GetType().Assembly;
            Stream aStream = aAsm.GetManifestResourceStream(sRes);
            constantAlphaImage = new Bitmap(aStream);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            // We are going to double buffer to cut down on flicker.
            // This isn't foolproof on slower devices (we can see the fullscreen Blt).
            // You could trade space for speed and keep this background buffer around
            // rather than (re)creating it each time Paint is called.  I would hook Resize and
            // create a new back buffer in that event.

            // The bitmap needs to be created with the 32bpp pixel format for the IImage to do the right thing.
            if (backBuffer != null)
            {




                // We beed a Graphics object on the buffer to get an HDC
                using (Graphics gxBuffer = Graphics.FromImage(backBuffer))
                {
                    // Since we nop'd OnPaintBackground, take care of it here
                    gxBuffer.Clear(this.BackColor);

                    // Ask the Image object from Imaging to draw itself.
                    IntPtr hdcDest = gxBuffer.GetHdc();
                    Rectangle dstRect = new Rectangle(100, 100, 148, 148);

                    gxBuffer.ReleaseHdc(hdcDest);

                    // AlphaBlend takes two HDC's - one source and one destination.  Here's the source.
                    using (Graphics gxSrc = Graphics.FromImage(constantAlphaImage))
                    {
                        IntPtr hdcDst = gxBuffer.GetHdc();
                        IntPtr hdcSrc = gxSrc.GetHdc();
                        BlendFunction blendFunction = new BlendFunction();
                        blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER;   // Only supported blend operation
                        blendFunction.BlendFlags = (byte)BlendFlags.Zero;           // Documentation says put 0 here
                        blendFunction.SourceConstantAlpha = alpha;// Constant alpha factor
                        blendFunction.AlphaFormat = (byte)0;                        // Don't look for per pixel alpha
                        PlatformAPIs.AlphaBlend(hdcDst, 0, 0, 200, 59, hdcSrc, 0, 0, 200, 59, blendFunction);
                        gxBuffer.ReleaseHdc(hdcDst);    // Required cleanup to GetHdc()
                        gxSrc.ReleaseHdc(hdcSrc);       // Required cleanup to GetHdc()
                    }

                    // Ask the Image object from Imaging to draw itself.
                    /*IntPtr*/
                    hdcDest = gxBuffer.GetHdc(); // This is redundant, but keeps the necessary code together
                    /*Rectangle*/

                    gxBuffer.ReleaseHdc(hdcDest);
                }

                // Put the final composed image on screen.
                e.Graphics.DrawImage(backBuffer, 19, 195);

            }
            //else
            //    e.Graphics.Clear(this.BackColor);
        }
        private void buttonLogin_Click(object sender, EventArgs e)
        {



        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void TimerLogoAnimation_Tick(object sender, EventArgs e)
        {


            this.timer++;
            if (this.timer > 4 && this.timer < 8)
            {
                alpha += 50;
                if (alpha >= 150)
                {
                    alpha = 255;
                    this.Refresh();

                }
                else
                    this.Refresh();
            }
            else if (this.timer > 10)
            {

                this.TimerLogoAnimation.Enabled = false;
                this.controller.performUserInput(TwitterClientController.STARTANIMATION_FINISH, null);
            }

        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PanelStartBackground));
            this.pictureTwitterBird = new System.Windows.Forms.PictureBox();
            this.TimerLogoAnimation = new System.Windows.Forms.Timer();
            this.SuspendLayout();
            // 
            // pictureTwitterBird
            // 
            this.pictureTwitterBird.Image = ((System.Drawing.Image)(resources.GetObject("pictureTwitterBird.Image")));
            this.pictureTwitterBird.Location = new System.Drawing.Point(47, 27);
            this.pictureTwitterBird.Name = "pictureTwitterBird";
            this.pictureTwitterBird.Size = new System.Drawing.Size(146, 141);
            // 
            // TimerLogoAnimation
            // 
            this.TimerLogoAnimation.Enabled = true;
            this.TimerLogoAnimation.Interval = 1000;
            this.TimerLogoAnimation.Tick += new System.EventHandler(this.TimerLogoAnimation_Tick);
            // 
            // PanelStartBackground
            // 
            this.Controls.Add(this.pictureTwitterBird);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
            this.ResumeLayout(false);

        }


    }
}
